package com.yupi.yuojbackendjudgeservice.judge.strategy;

import cn.hutool.json.JSONUtil;
import com.yupi.yuojbackendmodel.model.codesandbox.JudgeInfo;
import com.yupi.yuojbackendmodel.model.dto.question.JudgeCase;
import com.yupi.yuojbackendmodel.model.dto.question.JudgeConfig;
import com.yupi.yuojbackendmodel.model.entity.Question;
import com.yupi.yuojbackendmodel.model.enums.JudgeInfoMessageEnum;


import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class JavaLanguageJudgeStrategy implements JudgeStrategy {
    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {

        try {
            JudgeInfo judgeInfo = judgeContext.getJudgeInfo();
            List<String> inputListByQuestion = judgeContext.getInputList();
            List<String> outputListBySanbox = judgeContext.getOutputList();
            Question question = judgeContext.getQuestion();
            List<JudgeCase> judgeCasesList = judgeContext.getJudgeCasesList();
            Long memory = Optional.ofNullable(judgeInfo.getMemory()).orElse(0L);
            Long time = Optional.ofNullable(judgeInfo.getTime()).orElse(0L);
            JudgeInfo judgeInfoResponse = new JudgeInfo();
            judgeInfoResponse.setMemory(memory);
            judgeInfoResponse.setTime(time);

            //judgeInfoMessageEnum这个设置的是判题的结果 知道题目的判题结果是否正确与否 最后那里设置的是判题的状态
            JudgeInfoMessageEnum judgeInfoMessageEnum = JudgeInfoMessageEnum.ACCEPTED;
            List<String> outputListByQuestion = judgeCasesList.stream().map(JudgeCase::getOutput).collect(Collectors.toList());
            if (outputListBySanbox.size() != inputListByQuestion.size()) {
                judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
                judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
                return judgeInfoResponse;
            }
            for (int i = 0; i < judgeCasesList.size(); i++) {
                JudgeCase judgeCase = judgeCasesList.get(i);
                if (!judgeCase.getOutput().equals(outputListBySanbox.get(i))) {
                    judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
                    judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
                    return judgeInfoResponse;
                }
            }

            String judgeConfigStr = question.getJudgeConfig();
            JudgeConfig judgeConfig = JSONUtil.toBean(judgeConfigStr, JudgeConfig.class);
            Long needMemoryLimit = judgeConfig.getMemoryLimit();
            Long needTimeLimit = judgeConfig.getTimeLimit();

            if (memory > needMemoryLimit) {
                judgeInfoMessageEnum = JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED;
                judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
                return judgeInfoResponse;
            }
            //加入java程序执行需要加上而外的10秒钟
            long JAVA_PROGRAM_TIME_COST = 10000l;
            if ((time - JAVA_PROGRAM_TIME_COST)> needTimeLimit) {
                judgeInfoMessageEnum = JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED;
                judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
                return judgeInfoResponse;
            }

            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        } catch (Exception e) {
            return new JudgeInfo();
        }

    }

}